package com.zx.service.order.impl;

import com.zx.dao.order.OrderDetailsMapper;
import com.zx.dao.order.StateMapper;
import com.zx.dao.shop.ShoppingCartMapper;
import com.zx.dto.request.order.OrderDtoRequest;
import com.zx.dto.response.order.OrderDetailsDtoResult;
import com.zx.dto.response.order.OrderDetailsList;
import com.zx.model.order.OrderLog;
import com.zx.model.user.User;
import com.zx.service.goods.GoodsImgService;
import com.zx.service.order.OrderDetailsService;
import com.zx.service.order.StateService;
import com.zx.service.shop.ShoppingCartService;
import com.zx.util.CookieUtil;
import com.zx.util.MallConstant;
import com.zx.util.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sunkai
 * @date 2022/8/22 0022 16:35
 * @mood zhou xun
 */
@Service
public class OrderDetailsServiceImpl implements OrderDetailsService {

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Autowired
    private StateMapper stateMapper;

    @Override
    public OrderDetailsList queryOrderDetails(OrderDtoRequest orderDtoRequest, String userToken) {
        Integer index=orderDtoRequest.getPageIndex();
        OrderDetailsList orderDetailsList=new OrderDetailsList();
        User user = MallConstant.MAP.get(userToken);
        orderDtoRequest.setUserId(user.getId());
        orderDtoRequest.setPageIndex(PageUtil.pageNumber(orderDtoRequest.getPageIndex(),orderDtoRequest.getPageSize()));
        //总行数
        orderDetailsList.setTotalRows(orderDetailsMapper.OrderDetailsCount(orderDtoRequest));
        if (orderDetailsList.getTotalRows()<=0){
            return orderDetailsList;
        }
        orderDetailsList.setPageSize(orderDtoRequest.getPageSize());
        orderDetailsList.setTotalPage(PageUtil.totalPage(orderDetailsList.getTotalRows(),orderDetailsList.getPageSize()));
        orderDetailsList.setPageIndex(index);
        List<OrderDetailsDtoResult> orderDetailsDtoResults = orderDetailsMapper.queryOrderDetails(orderDtoRequest);
        if (orderDtoRequest.getOrderId()!=null){
            OrderDetailsList orderDetailsList1 = orderLogList(orderDtoRequest.getOrderId(), orderDetailsDtoResults, orderDetailsList);
            return orderDetailsList1;
        }
        orderDetailsList.setOrderDetailsDtoResultList(orderDetailsDtoResults);
        return orderDetailsList;
    }


    public OrderDetailsList orderLogList(Integer orderId,List<OrderDetailsDtoResult> orderDetailsDtoResults,
                                         OrderDetailsList orderDetailsList){
        List<OrderLog> orderLogList = stateMapper.queryOrderLog(orderId);
        if (CollectionUtils.isEmpty(orderLogList)) {
            return new OrderDetailsList();
        }
        Map<Integer,OrderDetailsDtoResult> map=
            orderDetailsDtoResults.stream().collect(Collectors.toMap(OrderDetailsDtoResult::getOrderId,a->a,(k1,k2)->k1));
            for (OrderLog log:orderLogList) {
                OrderDetailsDtoResult orderDetailsDtoResult = map.get(log.getOrderId());
                if (CollectionUtils.isEmpty(orderDetailsDtoResult.getOrderLogList())){
                    orderDetailsDtoResult.setOrderLogList(new ArrayList<>());
                }
                orderDetailsDtoResult.setOrderLogList(orderLogList);
            }
            orderDetailsList.setOrderDetailsDtoResultList(orderDetailsDtoResults);
       /* List<OrderLog> orderLogs = stateMapper.queryOrderLog(orderId);
        if (orderLogs!=null){
            List<OrderLog> orderLogList=new ArrayList<>();
           for (OrderDetailsDtoResult ordto : orderDetailsDtoResults) {
               for (OrderLog orderLog:orderLogs) {
                   OrderLog log=new OrderLog();
                   BeanUtils.copyProperties(orderLog,log);
                   orderLogList.add(log);
               }
               ordto.setOrderLogList(orderLogList);
           }
            orderDetailsList.setOrderDetailsDtoResultList(orderDetailsDtoResults);*/
           return orderDetailsList;
    }
}
